//
//  GRXDetailsController.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 16/1/14.
//  Copyright © 2016年 grx. All rights reserved.
//

#import "GRXDetailsController.h"
#import <WebKit/WebKit.h>
#import "GRXShareController.h"
@interface GRXDetailsController ()
@property (weak, nonatomic) WKWebView *webView;

@end

@implementation GRXDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //展示网页
    [self loadHtml];
    
    
    //监听加载标题
    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    
    
    //设置navgation
    [self setUpNavigation];
    
}

- (void)setUpNavigation{
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStyleDone target:self action:@selector(share)];
    
}

- (void)share{
    
    GRXShareController *shareController = [[GRXShareController alloc] init];
    
    [self presentViewController:shareController animated:YES completion:nil];
}

//只要监听到web对象的属性以改变就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    self.title = self.webView.title;
//    self.webView
}


//加载html网页
- (void)loadHtml{
    
    //创建WKWebView
    WKWebView *webView = [[WKWebView alloc] init];
    
    _webView = webView;
    
    [self.view addSubview:webView];
        
    //加载网页
    NSURL *url = [NSURL URLWithString:self.url];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [webView loadRequest:request];
}


//布局
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    _webView.frame = self.view.bounds;
}

- (void)dealloc{
    
    [self.webView removeObserver:self forKeyPath:@"title"];
    
    NSLog(@"销毁");
}

@end
